package com.example.userlogincenter.userCenter.config;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.example.userlogincenter.userCenter.entity.CenterUser;
import com.example.userlogincenter.userCenter.service.ICenterUserService;
import com.example.userlogincenter.userCenter.util.encrypUtils;
import org.apache.shiro.authc.*;
import org.apache.shiro.authz.AuthorizationInfo;
import org.apache.shiro.realm.AuthorizingRealm;
import org.apache.shiro.spring.web.ShiroFilterFactoryBean;
import org.apache.shiro.subject.PrincipalCollection;
import org.apache.shiro.web.mgt.DefaultWebSecurityManager;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import java.util.HashMap;
import java.util.Map;

@Configuration
public class ShiroConfig{

    @Bean
    public ShiroFilterFactoryBean shiroFilterFactoryBean(@Qualifier("securityManager") DefaultWebSecurityManager securityManager){
        ShiroFilterFactoryBean shiroFilterFactoryBean = new ShiroFilterFactoryBean();
        shiroFilterFactoryBean.setSecurityManager(securityManager);
        /*配置资源访问Map*/
        /**
         * -anon : 无需验证就能访问
         * -authc : 必须认证才能访问
         * -user : 必须开启rememberMe后才能用
         * -perms : 拥有对某个资源的权限才能访问
         * -role : 拥有某个角色才能访问
         * */
        Map<String, String> filterMap = new HashMap<>();
        filterMap.put("/swagger-ui/**","anon");
        filterMap.put("/swagger-ui","anon");
        filterMap.put("/swagger-resources/**","anon");
        filterMap.put("/webjars/**","anon");
        filterMap.put("/v2/**","anon");
        filterMap.put("/v3/**","anon");
        filterMap.put("/centerUser/*","anon");
        filterMap.put("/*","authc");
        shiroFilterFactoryBean.setFilterChainDefinitionMap(filterMap);
        return shiroFilterFactoryBean;
    }


    @Bean
    public DefaultWebSecurityManager securityManager(@Qualifier("userRealm") UserRealm userRealm){
        DefaultWebSecurityManager securityManager = new DefaultWebSecurityManager();
        securityManager.setRealm(userRealm);
        return securityManager;
    }

    @Bean
    public UserRealm userRealm(){
        return new UserRealm();
    }

}


//用户配置信息
class UserRealm extends AuthorizingRealm{

    @Autowired
    private ICenterUserService userService;


//    用户授权
    @Override
    protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principalCollection) {
        return null;
    }

//    用户认证
    @Override
    protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken authenticationToken) throws AuthenticationException {
        UsernamePasswordToken token = (UsernamePasswordToken) authenticationToken;
        QueryWrapper<CenterUser> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("phonenumber",token.getUsername());
        CenterUser one = userService.getOne(queryWrapper);
        if (one == null){
            return null;
        }
        String  desPass = null;
        desPass = one.getUserpassword();
        return new SimpleAuthenticationInfo("",desPass,"");
    }
}